import os,sys
import string

def splitfile(filename, splitnum):
    if (os.path.isfile(filename) == False):
        print 'action "add" datafile must exist'
        sys.exit(-1)

    filesize = os.path.getsize(filename)
    eachsize = filesize / string.atoi(splitnum)
    f = open(filename, "r")
    line = f.readline()
    f.seek(0, 0)
    linelen = len(line)
    lines = eachsize / linelen
    i = 0
    filename = 'tmp/%s' % (os.path.basename(filename),)
    while True:
        i += 1
        ff = open("%s.%d" % (filename, i), "w")
        for j in range(0, lines):
            line = f.readline()
            if not line:
                break
            ff.write(line)
        ff.close()
        if not line:
            break
        
if __name__ == '__main__':
    if len(sys.argv) < 2:
        print 'Usage arguments: file splitnum\n'
        sys.exit(-1)
    filename = sys.argv[1]
    splitnum = sys.argv[2]
    splitfile(filename, splitnum)
